//
//  CaptureView.swift
//  MyCloset
//
//  Created by 牟好好 on 2024/10/27.
//

import SwiftUI

struct CaptureView: View {
    @EnvironmentObject var captureViewModel: CaptureViewModel
    @State private var isShowingCamera = false
    @State private var showActionSheet = false
    @State private var isUploadPhoto = false
    
    var body: some View {
        VStack {
            if let capturedImage = captureViewModel.capturedImage {
                AnalysisView(image: capturedImage, isShowingCamera: $isShowingCamera, isUploadPhoto: $isUploadPhoto)
            } else {
                Button(action: {
                    showActionSheet = true
                }, label: {
                    Image(systemName: "camera.circle.fill")
                        .font(.system(size: 100))
                        .foregroundStyle(.accent)
                })
                .actionSheet(isPresented: $showActionSheet, content: {
                    ActionSheet(title: Text("选择获取照片方式"), message: nil, buttons: [
                        .cancel(),
                        .default(
                            Text("拍照上传"),
                            action: {
                                isUploadPhoto = false
                                isShowingCamera = true
                            }
                        ),
                        .default(
                            Text("从相册上传"),
                            action: {
                                isUploadPhoto = true
                                isShowingCamera = true
                            }
                        )
                    ])
                })
            }
        }
        .fullScreenCover(isPresented: $isShowingCamera) {
            CameraView(capturedImage: $captureViewModel.capturedImage, isPresented: $isShowingCamera, sourceType: isUploadPhoto ? .photoLibrary : .camera)
                .ignoresSafeArea(.all)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .padding(.top, safeAreaTop)
        .padding(.bottom, safeAreaBottom)
        .ignoresSafeArea(.all, edges: .all)
    }
}

#Preview {
    NavigationView {
        CaptureView()
            .environmentObject(CaptureViewModel())
    }
}

#Preview {
    NavigationView {
        CaptureView()
            .environmentObject(CaptureViewModel())
            .preferredColorScheme(.dark)
    }
}
